<template>
  <div class="scanner">
    <van-nav-bar title="RFID设置" left-text="返回" @click-left="onClickLeft"></van-nav-bar>
    <van-row type="flex" justify="center">
      <van-col span="24">
        <van-form class="common-form">
          <van-cell-group class="login_form">
            <van-field
              v-model="transmitPowerIndex"
              is-link
              readonly
              clickable
              label="扫描功率"
              placeholder="点击选择扫描功率"
              @click="showPopups.showTransmitPower = true"
            />
            <van-popup v-model="showPopups.showTransmitPower" round position="bottom">
              <van-picker
                show-toolbar
                :columns="transmitPowerIndexList"
                @confirm="onConfirm"
                @cancel="showPopups.showTransmitPower = false"
              />
            </van-popup>
          </van-cell-group>
        </van-form>
      </van-col>
    </van-row>
    <van-row  type="flex" justify="center" class="group_btn">
      <van-col span="24">
        <van-button
          class="reset_btn"
          type="primary"
          loading-type="spinner"
          block
          @click="saveScanSettings"
          >保存</van-button
        >
      </van-col>
    </van-row>
  </div>
</template>
<script>
import {mapGetters,mapActions} from "vuex";
export default {
  name: "scanSettings",
  computed: {
    ...mapGetters("scanner", ["pdaUseEnable", "pdaScanner"])
  },
  data() {
    return {
      transmitPowerIndex:200,
      showPopups: {
        showTransmitPower: false
      },
      transmitPowerIndexList:[50,100,150,200,250,300],
    };
  },
  beforeDestroy() {
    
  },
  created() {
    // 读取并且赋值
    if (this.$root.scanParams == undefined) {
      let scanParams = {};
      scanParams.transmitPowerIndex = 200;
      this.$root.scanParams = scanParams;
      this.transmitPowerIndex = this.$root.scanParams.transmitPowerIndex;
    } else {
      this.transmitPowerIndex = this.$root.scanParams.transmitPowerIndex;
    }
  },
  watch: {
  },
  methods: {
    ...mapActions("scanner", ["initScanEvent", "scanSuccessEvent", "scanFailEvent"]),
    // 保存结果 
    saveScanSettings(){
      this.initScanEvent(this.$config.scanner);
      if (this.pdaUseEnable) {
        let params = [];
        params.push(this.transmitPowerIndex);
        this.$root.scanParams.transmitPowerIndex = this.transmitPowerIndex;
        this.pdaScanner.scanSettings(window.plugins, this.scanSuccessEvent, this.scanFailEvent, params);
        this.$toast.success("保存成功");
      } else {
        this.$toast.fail("保存失败，获取设备扫描实例失败");
      }
      
    },
    // 返回上一级页面
    onClickLeft() {
      this.$router.push({
        path: "/home/index"
      });
    },

    // 选择功率
    onConfirm(value) {
      this.transmitPowerIndex = value;
      this.showPopups.showTransmitPower = false;
    },
  }
};
</script>

<style lang="less">
// .login_form {
//     //position: relative;
//     margin: 5px;
// }

.group_btn {
    padding: 5px;
}

</style>
